﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WcfBackend;

public partial class RecipeSearch : System.Web.UI.Page
{

    private static WcfFoodAndAllergiesClient wcf;
    private List<Allergies> AllergiList = new List<Allergies>();
    private List<Recipe> AllergyFriendlyRecipes = new List<Recipe>();




    protected void Page_Load(object sender, EventArgs e)
    {
        wcf = new WcfFoodAndAllergiesClient();
        DropDownListMenu.SelectedIndexChanged += new EventHandler(DropDownListMenu_SelectedIndexChanged);
        btnSearchRecipeByAllergy_Click(sender, e);
    }
    protected void btnSearchRecipeByAllergy_Click(object sender, EventArgs e)
    {
        string searchstring = txtSearchRecipe.Text;
        foreach (ListItem item in ChoosenAllergies.Items)
        {
            AllergiList.Add(wcf.GetAllergies(item.Text));
        }
        Allergies[] AllergiArray = AllergiList.ToArray();
        if (AllergiList.Count > 0)
        {
            lstSearchRecipe.Items.Clear();
            lstSearchRecipe.DataBind();
            AllergyFriendlyRecipes = wcf.GetAllergiFriendlyRecipies(AllergiArray).ToList();
        }
        else
        {
            AllergyFriendlyRecipes = wcf.SelectAllRecipe().ToList();
        }
        if (searchstring.Trim().Length > 0)
        {
            AllergyFriendlyRecipes = AllergyFriendlyRecipes.FindAll(r => r.Name.Contains(searchstring));
        }

        lstSearchRecipe.DataSource = AllergyFriendlyRecipes;
        lstSearchRecipe.DataBind();
        DropDownListMenu_SelectedIndexChanged(sender, e);
    }
    protected void DropDownListMenu_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownListMenu.SelectedValue != "Vælge Menu")
        {
            string menuType = DropDownListMenu.SelectedValue;
            List<Recipe> results = wcf.GetRecipeByType(menuType).ToList();


            lstSearchRecipe.DataSource = results;
            lstSearchRecipe.DataBind();
        }
    }

    protected void on_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)(sender);
        string id = btn.CommandArgument;

        Response.Redirect("Resultpage.aspx?" + "id=" + id);
    }
    protected void MoveLeft_Click(object sender, EventArgs e)
    {
        List<ListItem> output = new List<ListItem>();
        foreach (ListItem item in ChoosenAllergies.Items)
        {
            if (item.Selected == true)
            {
                ExtingAllergies.Items.Add(item);
                output.Add(item);
                item.Selected = false;

            }

        }
        foreach (ListItem item in output)
        {
            ChoosenAllergies.Items.Remove(item);
        }
        SortExt();



    }
    protected void MoveRight_Click(object sender, EventArgs e)
    {
        List<ListItem> output = new List<ListItem>();
        foreach (ListItem item in ExtingAllergies.Items)
        {
            if (item.Selected == true)
            {
                ChoosenAllergies.Items.Add(item);
                output.Add(item);
                item.Selected = false;

            }

        }
        foreach (ListItem item in output)
        {
            ExtingAllergies.Items.Remove(item);
        }
        SortChosen();
    }

    private void SortChosen()
    {
        List<Allergies> chosen = new List<Allergies>();
        foreach (ListItem item in ChoosenAllergies.Items)
        {
            chosen.Add(wcf.GetAllergies(item.Text));
        }
        var sorted = chosen.OrderBy(i => i.Name).ToList();
        ChoosenAllergies.Items.Clear();
        foreach (Allergies a in sorted)
        {
            ChoosenAllergies.Items.Add(a.Name);
        }
    }
    private void SortExt()
    {
        List<Allergies> ext = new List<Allergies>();
        foreach (ListItem item in ExtingAllergies.Items)
        {
            ext.Add(wcf.GetAllergies(item.Text));
        }
        var sorted = ext.OrderBy(i => i.Name).ToList();
        ExtingAllergies.Items.Clear();
        foreach (Allergies a in sorted)
        {
            ExtingAllergies.Items.Add(a.Name);
        }
    }
}

